package com.homegoods.area.item;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.homegoods.area.model.AreaModel;
import com.homegoods.resource.R;
import com.kedll.library.adapter.MultiSelectAdapter;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.BaseItem;

/**
 * @author QiQi
 * @ClassName: AreaItem
 * @date 2018/10/2
 * @description
 */
public class AreaItem extends BaseItem<AreaModel> {
    private MultiTypeAdapter adapter;

    public AreaItem(AreaModel data, MultiTypeAdapter adapter) {
        super(data);
        this.adapter = adapter;
        setOnClickListener(v -> {
            getAdapter().toggle(this);
            setSelected(!data.isSelected());
            adapter.notifyItemRangeChanged(0, adapter.getItemCount());
            if (adapter.getOnItemClickListener() != null) {
                adapter.getOnItemClickListener().onItemClicked(v, this);
            }
        });
    }

    private MultiSelectAdapter<AreaItem> getAdapter() {
        return (MultiSelectAdapter) adapter;
    }

    public boolean isSelected() {
        return getData().isSelected();
    }

    private void setSelected(boolean selected) {
        for (MultiTypeAdapter.IItem item : getAdapter().getValues()) {
            ((AreaItem) item).getData().setSelected(false);
        }
        getData().setSelected(selected);

    }

    public String getAreaName() {
        return getData().getAreaName();
    }


    @Override
    public int getLayout() {
        return R.layout.area_item;
    }
}
